<%@ page language="java" import="java.util.*,com.mongodb.*,java.util.regex.Pattern" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
         response.setContentType("text/html;charset=utf-8");  
         request.setCharacterEncoding("utf-8");           
         String pageNo=(String)request.getParameter("pageNo");  
         String num=(String)request.getParameter("size");
         String search=(String)request.getParameter("search");
         int size = Integer.parseInt(num);
         int currentpage = Integer.parseInt(pageNo);
         String time = request.getParameter("time");
         String see = request.getParameter("see");
         String author = request.getParameter("author");
	   	  try{   
	             // 连接到 mongodb 服务
	             MongoClient mongoClient = new MongoClient( "localhost" , 27017 );
	             DB db = mongoClient.getDB( "blog" );  //连接到数据库blog
	             DBCollection coll = db.getCollection("article");  //获取blog数据库中集合user
	            
            	if(search==""){
            		BasicDBObject query = new BasicDBObject();	
            		System.out.println(author);
            		if(author!=null){
            			System.out.println("author");
            			System.out.println(author);
            			query.put("username",author);
            			DBCursor articles = coll.find(query).limit(size).skip(size*(currentpage-1));
            			List<DBObject> list = articles.toArray();
	   	            	 DBObject e = new BasicDBObject();
	   	            	 int total = coll.find(query).length()/size + 1;
	   	            	 if(coll.find(query).length()%size==0){
	   	            		 total = coll.find(query).length()/size;
	   	            	 }
	   			         e.put("totalpage",total);
	   			         e.put("currentpage",currentpage);
	   			         e.put("numberOfElement",articles.length());
	   	            	 list.add(e);
	   	            	 response.getWriter().write(list.toString());
	   		             response.getWriter().close();
            		}else{
            			System.out.println("no author");
            			if(time!=null && time.equals("new")){
                			query.put("createtime",-1);
                		}
                		else if(time!=null && time.equals("old")){
                			query.put("createtime",1);
                		}
                		else if(see!=null && see.equals("hot")){
                			query.put("readNum",-1);
                		}
                		else if(see!=null && see.equals("cold")){
                			query.put("readNum",1);
                		}
                		DBCursor articles = coll.find().sort(query).limit(size).skip(size*(currentpage-1));
    	            	 List<DBObject> list = articles.toArray();
    	            	 DBObject e = new BasicDBObject();
    	            	 int total = coll.find().length()/size + 1;
    	            	 if(coll.find().length()%size==0){
    	            		 total = coll.find().length()/size;
    	            	 }
    			         e.put("totalpage",total);
    			         e.put("currentpage",currentpage);
    			         e.put("numberOfElement",articles.length());
    	            	 list.add(e);
    	            	 response.getWriter().write(list.toString());

    		             response.getWriter().close();
            		}
                }
                else{
                	 
                	 Pattern pattern = Pattern.compile("^.*" + search + ".*$", Pattern.CASE_INSENSITIVE);
                	 BasicDBObject query = new BasicDBObject();
                	 query.put("title",pattern);
                	 //对文章标题的模糊查询
                	 DBCursor articles = coll.find(query).limit(size).skip(size*(currentpage-1));
	            	 List<DBObject> list = articles.toArray();
	            	 DBObject e = new BasicDBObject();
	            	 int total = coll.find(query).length()/size + 1;
	            	 if(coll.find().length()%size==0){
	            		 total = coll.find().length()/size;
	            	 }
			         e.put("totalpage",total);
			         e.put("currentpage",currentpage);
			         e.put("numberOfElement",articles.length());
	            	 list.add(e);
	            	 response.getWriter().write(list.toString());
		             response.getWriter().close();
                }
	            
	          }catch(Exception e){
	       	     response.getWriter().write("false");
	       	     response.getWriter().close();
	             System.err.println( e.getClass().getName() + ": " + e.getMessage() );
	         }    
          
     %>
</body>
</html>